package com.qxzn.module.home.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.View;

import com.alibaba.fastjson.JSONObject;
import com.github.jdsjlzx.interfaces.OnItemClickListener;
import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.qxzn.app.api.api.HttpRequest;
import com.qxzn.app.bean.AlbumPictureBean;
import com.qxzn.app.bean.BaseBean;
import com.qxzn.app.page.BaseActivity;
import com.qxzn.app.page.interfaces.MyInterface;
import com.qxzn.app.page.model.HomeModel;
import com.qxzn.app.page.util.ImageUtil;
import com.qxzn.app.util.AppUtil;
import com.qxzn.app.util.SharePreferenceUtil;
import com.qxzn.module.home.R;
import com.qxzn.module.home.R2;
import com.qxzn.module.home.adapter.AlbumPictureAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class PictureShareActivity extends BaseActivity implements MyInterface.AlbumPictureClickListener, OnRefreshListener, OnLoadMoreListener, MyInterface.GetAlbumListListener, MyInterface.DelAlbumPictureListener, OnItemClickListener {
    @BindView(R2.id.lrv_picture)
    LRecyclerView lrvPicture;
    @BindView(R2.id.view_empty)
    View viewEmpty;
    @BindView(R2.id.recycler_pager)
    ViewPager pager;

    private List<AlbumPictureBean> dataList = new ArrayList<>();
    private AlbumPictureAdapter albumPictureAdapter;
    private LRecyclerViewAdapter lRecyclerViewAdapter;
    private HomeModel homeModel;
    private int page = 1;
    private int pageSize = 10;
    private ArrayList<String> imgList = new ArrayList<>();

    @Override
    protected int initLayout() {
        return R.layout.activity_picture_share;
    }

    @Override
    protected void initView() {
        viewEmpty.setOnClickListener(this);
        lrvPicture.setLayoutManager(new LinearLayoutManager(this));
        albumPictureAdapter = new AlbumPictureAdapter(this, dataList);
        albumPictureAdapter.setPictureClick(this);
        lRecyclerViewAdapter = new LRecyclerViewAdapter(albumPictureAdapter);
        lrvPicture.setEmptyView(viewEmpty);
        lrvPicture.setAdapter(lRecyclerViewAdapter);
        lRecyclerViewAdapter.setOnItemClickListener(this);
//        sourceImageList.add("http://pic33.nipic.com/20131007/13639685_123501617185_2.jpg");
//        sourceImageList.add("http://pic25.nipic.com/20121112/9252150_150552938000_2.jpg");
//        sourceImageList.add("http://pic16.nipic.com/20111006/6239936_092702973000_2.jpg");
//        sourceImageList.add("http://pic1.win4000.com/wallpaper/c/53cdd1f7c1f21.jpg");
//        initPage();
    }

    @Override
    protected void iniTitleBar() {
        tv_title.setText("照片分享");
        tv_right.setVisibility(View.VISIBLE);
        tv_right.setText("选择");
        tv_right.setTextColor(Color.parseColor("#2A8CFF"));
        tv_right.setOnClickListener(this);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        homeModel = new HomeModel(this);
        homeModel.setGetAlbumListListener(this);
        homeModel.setDelAlbumPictureListener(this);
        getAlbumPicture();
    }

    private void getAlbumPicture() {
        showProgress(true);
        JSONObject params = new JSONObject();
        params.put("device_id", SharePreferenceUtil.getDeviceId(this));
        params.put("username", SharePreferenceUtil.getUserName(this));
        homeModel.getAlbumList(params);
    }

    private void delAlbumPicture() {
        JSONObject params = new JSONObject();
        params.put("id", "");
        homeModel.getAlbumList(params);
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        if (view.getId() == R.id.tv_right) {

        } else if (view.getId() == R.id.view_empty) {
            getAlbumPicture();
        }
    }

    @Override
    public void onGetAlbumListSuccess(BaseBean<List<AlbumPictureBean>> baseBean) {
        hideProgress();
        if (baseBean != null) {
            if (page == 1) {
                dataList.clear();
            }
            List<AlbumPictureBean> list = baseBean.getData();
            if (list != null) {
                if (list.size() == 0) {
                    lrvPicture.setNoMore(true);
                }
                dataList.addAll(list);
                dataList.addAll(dataList);
                dataList.addAll(dataList);
                dataList.addAll(dataList);
            }
            albumPictureAdapter.notifyDataSetChanged();
            lRecyclerViewAdapter.notifyDataSetChanged();
            for (AlbumPictureBean albumPictureBean : dataList) {
                for (AlbumPictureBean.ResultheadBean resultheadBean : albumPictureBean.getResulthead()) {
                    imgList.add(HttpRequest.BASE_URL + resultheadBean.getPhoto());
                }
            }
        }
    }

    @Override
    public void onGetAlbumListFailure() {
        hideProgress();
    }

    @Override
    public void onDelAlbumPictureSuccess(BaseBean baseBean) {
        if (baseBean != null && baseBean.getCode() == 200) {
            if (!TextUtils.isEmpty(baseBean.getMsg())) {
                AppUtil.showToast(this, baseBean.getMsg());
            } else {
                AppUtil.showToast(this, "删除图片成功");
            }
            getAlbumPicture();
        } else {
            if (baseBean != null && !TextUtils.isEmpty(baseBean.getMsg())) {
                AppUtil.showToast(this, baseBean.getMsg());
            } else {
                AppUtil.showToast(this, "删除图片失败");
            }
        }
    }

    @Override
    public void onDelAlbumPictureFailure() {

    }

    @Override
    public void onLoadMore() {
        if (dataList.size() % pageSize == 0) {
            page++;
            getAlbumPicture();
        } else {
            lrvPicture.setNoMore(true);
        }
    }

    @Override
    public void onRefresh() {
        page = 1;
        getAlbumPicture();
    }

    @Override
    public void onItemClick(View view, int position) {
//        animator.enter(position, true);
    }

    @Override
    public void onAlbumPictureClickSuccess(View currentView, int itemPosition, int subItemPosition) {
        int position = 0;
        for (int i = 0; i < itemPosition + 1; i++) {
            if (i == itemPosition) {
                position += subItemPosition;
            } else {
                position += dataList.get(i).getResulthead().size();
            }
        }
        ImageUtil.viewImg(this, imgList, currentView, position);
    }
}
